package com.yanqu.road.dao.impl.activity.dafuweng;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.activity.dafuweng.DFWParamConfig;
import com.yanqu.road.entity.enums.activity.dafuweng.eDaFuWengParamType;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class DFWParamConfigDaoImpl extends TempDao {

    private static final String sql = "select * from t_s_monopoly_config where activityId = ? order by id asc";

    public Map<eDaFuWengParamType, DFWParamConfig> getDFWParamConfigMap(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<eDaFuWengParamType, DFWParamConfig> map = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(sql);
            call.setInt(1, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                DFWParamConfig config = new DFWParamConfig();
                config.setId(rs.getInt("id"));
                config.setParam(rs.getString("param"));
                config.setValue(rs.getString("value"));
                eDaFuWengParamType type = eDaFuWengParamType.getType(config.getParam());
                if (type != null) {
                    map.put(type, config);
                }
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

}
